<template>
  <el-header>
    <div class="toolbar">
      <!-- 左侧Logo区域 -->
      <div class="logo-container">
        <img src="@/assets/logo.svg" alt="Logo" class="logo" />
      </div>

      <!-- 中间搜索框 -->
      <div class="search-container">
        <el-input
          placeholder="请输入搜索内容"
          :prefix-icon="Search"
          clearable
          class="search-input"
        />
      </div>

      <!-- 右侧功能区 -->
      <div class="function-container">
        <div class="icon-group">
          <el-tooltip content="语言切换" placement="bottom">
            <el-icon class="header-icon"><Reading /></el-icon>
          </el-tooltip>

          <!-- 消息图标组件 -->
          <MessageIcon />

          <el-tooltip content="通知" placement="bottom">
            <el-icon class="header-icon"><bell /></el-icon>
          </el-tooltip>

          <el-tooltip content="帮助" placement="bottom">
            <el-icon class="header-icon"><question-filled /></el-icon>
          </el-tooltip>

          <el-tooltip :content="isDark ? '切换到亮色模式' : '切换到暗色模式'" placement="bottom">
            <div class="theme-switch" @click="toggleDarkMode">
              <el-icon class="header-icon">
                <Moon v-if="!isDark" />
                <Sunny v-else />
              </el-icon>
            </div>
          </el-tooltip>
        </div>

        <!-- 用户信息下拉菜单 -->
        <el-dropdown class="user-dropdown">
          <div class="user-info">
            <el-avatar size="small" class="user-avatar">TD</el-avatar>
            <span class="username">TDesign Admin</span>
            <el-icon><arrow-down /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item>设置</el-dropdown-item>
              <el-popconfirm title="确认退出吗?" confirm-button-text="确认" cancel-button-text="取消">
                <template #reference>
                  <el-dropdown-item>退出登录</el-dropdown-item>
                </template>
              </el-popconfirm>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </el-header>
</template>

<script setup lang="ts">
import { Setting, Search, Promotion, Bell, QuestionFilled, ArrowDown, Reading, Moon, Sunny } from '@element-plus/icons-vue'
import MessageIcon from '@/views/doctorViews/message/components/MessageIcon.vue'
import { ref, onMounted } from 'vue'
import 'element-plus/theme-chalk/dark/css-vars.css'

// 暗黑模式状态
const isDark = ref(false)

// 初始化暗黑模式状态
onMounted(() => {
  // 从本地存储中获取暗黑模式状态
  const darkMode = localStorage.getItem('vitallink-dark-mode')
  if (darkMode === 'dark') {
    document.documentElement.classList.add('dark')
    isDark.value = true
  }
})

// 切换暗黑模式
const toggleDarkMode = () => {
  // 切换状态
  isDark.value = !isDark.value

  // 根据状态添加或移除dark类
  if (isDark.value) {
    document.documentElement.classList.add('dark')
    localStorage.setItem('vitallink-dark-mode', 'dark')
  } else {
    document.documentElement.classList.remove('dark')
    localStorage.setItem('vitallink-dark-mode', 'light')
  }
}
</script>


<style scoped lang="scss">
.el-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 60px;
  z-index: 1000;
  background: linear-gradient(to right, #f5f7fa, #819ff0);
  color: var(--el-text-color-primary);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  padding: 0;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 100%;
  padding: 0 16px;
}

.logo-container {
  display: flex;
  align-items: center;
  height: 100%;
  min-width: 120px;
}

.logo {
  height: 32px;
}

.search-container {
  flex: 1;
  display: flex;
  justify-content: center;
  padding: 0 20px;
  max-width: 500px;
}

.search-input {
  width: 100%;
  max-width: 400px;

  :deep(.el-input__wrapper) {
    border-radius: 20px;
    padding: 0 15px;
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 0 0 1px var(--el-color-primary);
    }
  }
}

.function-container {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-group {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-right: 16px;
}

.header-icon {
  font-size: 22px;
  color: #333;
  cursor: pointer;
  transition: color 0.3s ease;

  &:hover {
    color: var(--el-color-primary);
  }
}

.badge-item {
  height: 20px;
  line-height: 20px;
}

.user-dropdown {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-avatar {
  background-color: var(--el-color-primary);
}

.username {
  font-size: 14px;
  color: #333;
}
</style>
